package o.json;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSONObject extends JSONValue {
	private final Map<JSONString, JSONValue> values = new HashMap<JSONString, JSONValue>();

	public JSONObject() {
	}

	public Collection<JSONString> getKeys() {
		final List<JSONString> keys = new ArrayList<JSONString>(values.keySet());
		Collections.sort(keys, new Comparator<JSONString>() {
			@Override
			public int compare(JSONString o1, JSONString o2) {
				return o1.getValue().compareTo(o2.getValue());
			}
		});
		return keys;
	}
	public JSONValue getValue(JSONString key) {
		return values.get(key);
	}
	public void setValue(JSONString key, JSONValue value) {
		if (key.isEmpty()) throw new IllegalArgumentException();

		values.put(key, value);
	}

	@Override
	public boolean isObject() {
		return true;
	}
	@Override
	public JSONObject getObject() throws JSONException {
		return this;
	}

	@Override
	public boolean equals(Object object) {
		if (object == this) return true;
		if (object instanceof JSONObject) {
			return equals((JSONObject) object);
		}
		return false;
	}
	public boolean equals(JSONObject that) {
		return this.values.equals(that.values);
	}
	@Override
	public int hashCode() {
		return values.hashCode();
	}

	@Override
	public String toString() {
		return "[Object "+ values +"]";
	}
}
